home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Risc World 3
/
Risc World 3.iso
/
SOFTWARE
/
ISSUE3
/
PD
/
NEWALARM
/
!NewAlarm
/
Source
/
SHeap
(
.txt
)
< prev
Wrap
RISC OS BBC BASIC V Source
|
2000-10-05
|
6KB
|
195 lines
Sliding_Heap Library
requires SlidingHeap 2.00
module and PROCs
Steven Haslam 1992
initheaps(heapsize%,slidingblocks%)
Call this procedure to create the empty heap before you do anything else
fixedheapsize%=heapsize%
Lheap_trigger%=
_heap_pageup(
+fixedheapsize%+20+20*slidingblocks%-&8000)
setslotsize(heap_trigger%)
_heap_slotsize<heap_trigger%
130,"Unable to initialise heap"
fixedheapbase%=
%slidingheapbase%=
+fixedheapsize%
"OS_Heap",0,fixedheapbase%,,fixedheapsize%
"SlidingHeap_Create",slidingheapbase%,2,slidingblocks%
"SlidingHeap_VerifyHeap",slidingheapbase%
create_anchor(name$)
Every sliding block must have an anchor. The block gets moved around
in memory, but its current address can always be found at !anchor%
This function creates an anchor and returns its address,
which should be assigned to a variable anchor% for future reference.
The name you supply will be used in error messages and heap info reports
space%
space% 4+
name$+1
!space%=0
$(space%+4)=name$
=space%
create_named_sliding_block(anchor%,size%)
This is the function which actually creates a heap block.
First you must have created an anchor for the block.
trysize%
size%=
_heap_wordup(size%)
#7trysize%=
_heap_pageup(
_heap_nextfree+size%-&7FF4)
trysize%>heap_trigger%
setslotsize(trysize%)
_heap_slotsize<trysize%
'#
setslotsize(heap_trigger%)
(D
131,"Not enough room to create block """+$(anchor%+4)+""""
)
heap_trigger%=trysize%
"SlidingHeap_NewBlock",slidingheapbase%,anchor%,size%,anchor%+4
"SlidingHeap_VerifyHeap",slidingheapbase%
extend_named_sliding_block(anchor%,newsize%)
This function increases (or decreases - the name is misleading) the size
of a heap block. Other blocks may slide around as the block size changes.
If the block did not previously exist, it will be created - so there's not
really a lot of point using PROCcreate_named_sliding_block, I suppose...
Note that newsize% is the TOTAL size of the resulting block, not the
increase/decrease in size - i.e. you need to keep track of the current size.
!anchor%=0
create_named_sliding_block(anchor%,newsize%):
!anchor%>
_heap_nextfree
129,"Block beyond heap limits"
:$newsize%=
_heap_wordup(newsize%)
"SlidingHeap_DescribeBlock",slidingheapbase%,anchor%
,,oldsize%
larger%=newsize%>oldsize%
larger%
>G trysize%=
_heap_pageup(
_heap_nextfree+(newsize%-oldsize%)-&7FFC)
trysize%>heap_trigger%
setslotsize(trysize%)
A$
_heap_slotsize<trysize%
B%
setslotsize(heap_trigger%)
C=
132,"Not enough room to extend block #"+
~anchor%
E heap_trigger%=trysize%
F
"SlidingHeap_ExtendBlock",slidingheapbase%,anchor%,newsize%
J1trysize%=
_heap_pageup(
_heap_nextfree-&7FFC)
trysize%<>heap_trigger%
setslotsize(trysize%)
heap_trigger%=trysize%
"SlidingHeap_VerifyHeap",slidingheapbase%
scrap_sliding_block(anchor%)
This function discards a sliding block, returning its memory to the heap.
Note that the anchor is NOT deleted and can be reused later.
In fact anchors cannot be deleted.
!anchor%=0
"SlidingHeap_ScrapBlock",slidingheapbase%,anchor%
X1trysize%=
_heap_pageup(
_heap_nextfree-&7FFC)
trysize%<>heap_trigger%
setslotsize(trysize%)
heap_trigger%=trysize%
!anchor%=0
"SlidingHeap_VerifyHeap",slidingheapbase%
destroyheaps
This procedure destroys the entire sliding heap.
setslotsize(
-&8000)
Various procedures called by Sliding Heap Library procedures
_heap_slotsize
"Wimp_SlotSize",-1,-1
_heap_pageup(n%)
"OS_ReadMemMapInfo"
=(n%+R0%-1)
(R0%-1)
setslotsize(newsize%)
"Wimp_SlotSize",newsize%,-1
_heap_nextfree
nextfree%
"SlidingHeap_NextFree",slidingheapbase%
nextfree%
=nextfree%
_heap_wordup(x%)=(x%+3)
heap_store(anchor%,
size%,inc%,
ptr%,L%,string$)
string$<>""
(string$)
ptr%-!anchor%+L%+1>size%
size%+=inc%
extend_named_sliding_block(anchor%,size%)
string$<>""
$ptr%=string$:ptr%+=L%:?ptr%=10
_heap_numtostr(d%,n%)=
d%,"0")+
~n%,d%)
_heap_snumtostr(d%,n%)=
d%," ")+
n%,d%)
heapsinfo
"OS_Heap",1,fixedheapbase%
,,bigbloc%,totfree%
"Fixed heap
CTRL-O : pause scrolling until SHIFT
"----- ----"
"Heap base : &";
_heap_numtostr(8,fixedheapbase%)
"Heap size : ";
_heap_bytes2(fixedheapsize%)
"Largest free : ";
_heap_bytes2(bigbloc%)
"Total free : ";
_heap_bytes2(totfree%)
"Sliding heap"
"------- ----"
"SlidingHeap_HeapInfo",slidingheapbase%
_heap_bytes(b%)
end%
"OS_ConvertFixedFileSize",b%,block%,block%+&100
,end%
?end%=13
=$block%
_heap_bytes2(b%)
end%
"OS_ConvertFileSize",b%,block%,block%+&100
,end%
?end%=13
=$block%
create_fixed_block(size%)
pointer%,flag%
"XOS_Heap",2,fixedheapbase%,,size%
,,pointer%;flag%
flag%
extendfixedheap
"XOS_Heap",2,fixedheapbase%,,size%
,,pointer%;flag%
=pointer%
extendfixedheap
nshb%,extend%,trysize%
"OS_ReadMemMapInfo"
extend%
% trysize%=
_heap_slotsize+extend%
setslotsize(trysize%)
_heap_slotsize<trysize%
255,"No room to extend fixed heap"
# nshb%=slidingheapbase%+extend%
"SlidingHeap_ShiftHeap",slidingheapbase%,nshb%
"OS_Heap",5,fixedheapbase%,,extend%
fixedheapsize%+=extend%
slidingheapbase%=nshb%
"SlidingHeap_VerifyHeap",slidingheapbase%